home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libio / ioextend.cc < prev    next >
C/C++ Source or Header  |  1993-08-17  |  4KB  |  130 lines

  1. /* 
  2. Copyright (C) 1993 Free Software Foundation
  3.  
  4. This file is part of the GNU IO Library.  This library is free
  5. software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU CC; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. As a special exception, if you link this library with files
  20. compiled with a GNU compiler to produce an executable, this does not cause
  21. the resulting executable to be covered by the GNU General Public License.
  22. This exception does not however invalidate any other reasons why
  23. the executable file might be covered by the GNU General Public License. */
  24.  
  25. #include <iostream.h>
  26.  
  27. static int __xalloc = 0;
  28.  
  29. int ios::xalloc()
  30. {
  31.   return __xalloc++;
  32. }
  33.  
  34. static ios::fmtflags __used_fmt_flags
  35. = ios::skipws | ios::left | ios::right | ios::internal
  36. | ios::dec | ios::oct | ios::hex | ios::showbase | ios::showpoint
  37. | ios::uppercase | ios::showpos | ios::scientific | ios::fixed
  38. | ios::unitbuf | ios::stdio | ios::dont_close;
  39.  
  40. ios::fmtflags ios::bitalloc()
  41. {
  42.   fmtflags bit_to_try = (fmtflags)1;
  43.   for (; bit_to_try; bit_to_try <<= 1)
  44.     {
  45.       if ((__used_fmt_flags & bit_to_try) == 0)
  46.     {
  47.       __used_fmt_flags |= bit_to_try;
  48.       return bit_to_try;
  49.     }
  50.     }
  51.   return 0;
  52. }
  53.  
  54. // NOTE:  This implementation of ios::iword and ios::pword assumes
  55. // that these methods are seldom used, so we want to minimize
  56. // the space and time overhead when NOT using these methods.
  57. //
  58. // ANSI specifies two conceptually-infinite arrays, one whose
  59. // elements are longs, and one whose elements are (void*)s.
  60. // We implement this as a single array, each of whose element is
  61. // a (struct ptr_and_long), which has space for both a long and a void*.
  62. // We also specify that (the i field of) the 0'th element of the array
  63. // contains the allocated length of the array (not counting that
  64. // initial element).
  65.  
  66. struct ptr_and_long {
  67.   void *p;
  68.   long i;
  69. };
  70.  
  71. static struct ptr_and_long&
  72. get_array_element(ios& io, int index)
  73. {
  74.   if (index < 0)
  75.     io._throw_failure();
  76.   register struct ptr_and_long *array = (ptr_and_long*)io._arrays;
  77.   int old_length = array == NULL ? 0 : array[0].i;
  78.   if (index >= old_length)
  79.     {
  80.       register int i;
  81.       int new_length = index + 10;
  82.       register struct ptr_and_long *new_array
  83.     = new ptr_and_long[1 + new_length];
  84.       if (array != NULL)
  85.     {
  86.       for (i = 1; i <= old_length; i++)
  87.         new_array[i] = array[i];
  88.       delete [] array;
  89.     }
  90.       for (i = old_length + 1; i <= new_length; i++)
  91.     {
  92.       new_array[i].i = 0;
  93.       new_array[i].p = (void*)0;
  94.     }
  95.       new_array[0].i = new_length;
  96.       new_array[0].p = (void*)0;
  97.       io._arrays = (void*)new_array;
  98.       array = new_array;
  99.     }
  100.   return array[index+1];
  101. }
  102.  
  103. long& ios::iword(int index)
  104. {
  105.   return get_array_element(*this, index).i;
  106. }
  107.  
  108. void*& ios::pword(int index)
  109. {
  110.   return get_array_element(*this, index).p;
  111. }
  112.  
  113. long ios::iword(int index) const
  114. {
  115.   if (index < 0)
  116.     _throw_failure();
  117.   register struct ptr_and_long *pl_array = (ptr_and_long*)_arrays;
  118.   int len = pl_array == NULL ? 0 : pl_array[0].i;
  119.   return index >= len ? 0 : pl_array[index+1].i;
  120. }
  121.  
  122. void* ios::pword(int index) const
  123. {
  124.   if (index < 0)
  125.     _throw_failure();
  126.   register struct ptr_and_long *pl_array = (ptr_and_long*)_arrays;
  127.   int len = pl_array == NULL ? 0 : pl_array[0].i;
  128.   return index >= len ? 0 : pl_array[index+1].p;
  129. }
  130.